package Dao;

import Entidade.PwDSM;
import Entidade.Usuario;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.swing.JOptionPane;

public class Dao {

    public Connection getConnection(){
        try {
            System.out.println("Conectando...");
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:jtds:sqlserver://172.16.0.209/evasor", "evasor", "crtevasor2014");
            //Connection con = DriverManager.getConnection("jdbc:jtds:sqlserver://172.16.0.209/scc-dsv", "sccdsv", "crtsccdsv2011");
            //Connection con = DriverManager.getConnection("jdbc:jtds:sqlserver://172.16.0.209/teste", "scc", "crtscc2011");
            System.out.println("Conectado a VirtualBD/SCC!");
            return con;
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex);
            return null;
        }
    }

    public Statement getStatement() throws SQLException{
        return getConnection().createStatement();
    }

    public PreparedStatement getStatement(String st) throws SQLException{
        return getConnection().prepareStatement(st);
    }

    public ResultSet executeQuery(String query, Object... params) throws SQLException{
        PreparedStatement ps = getStatement(query);        
        for (int i = 0; i < params.length; i++){
            ps.setObject(i+1, params[i]);
        }        
        return ps.executeQuery();
    }

    public int executaComando(String query, Object... params) throws SQLException{
        PreparedStatement ps = getStatement(query);
        for (int i = 0; i < params.length; i++){
            ps.setObject(i+1, params[i]);
        }
        int result = ps.executeUpdate();
        return result;
    }
    //public void addUser(String nome, String login, String senha) throws SQLException, BadPaddingException, NoSuchPaddingException, IllegalBlockSizeException, InvalidKeyException, NoSuchAlgorithmException, InvalidAlgorithmParameterException{
    //    String senhaCriptografada = PwDSM.encrypt(senha);
    //    String query = "EXEC dbo.SP_CadastrarUsuario ?, ?, ?"; //nome, login, senha
    //    executeCommand(query, nome, login, senhaCriptografada);
    //}
    
     public int registraEvasao(String pista, String placa, String dataEvasao, String cat, String atender, String loginUsuario, int SeqTransito) throws SQLException, BadPaddingException, NoSuchPaddingException, IllegalBlockSizeException, InvalidKeyException, NoSuchAlgorithmException, InvalidAlgorithmParameterException{
        String query = "EXEC dbo.SP_REGISTRA_EVASAO ?, ?, ?, ?, ?, ?, ?";
        int x = executaComando(query, pista, placa, dataEvasao, cat, atender, loginUsuario, SeqTransito);   
        return x;
    }
            
    public boolean validaLogon(String loginUsuario, String senhaUsuario){
        boolean toReturn = false;
        String senha = "";
        try {
            senha = PwDSM.encrypt(senhaUsuario);
            ResultSet rs = executeQuery("SELECT * FROM USUARIOS WHERE LOGIN=? AND SENHA = ? AND STATUS = 0", loginUsuario, senha);
            if (rs.next()){
                toReturn = true;
            }
            rs.close();            
        } catch (SQLException ex) {
            Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (BadPaddingException ex) {
            Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchPaddingException ex) {
            Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalBlockSizeException ex) {
            Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidKeyException ex) {
            Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidAlgorithmParameterException ex) {
            Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return toReturn;
    }    
    
    public Usuario obterDadosUsuario(String loginUsuario) throws SQLException{
        
        ResultSet rs = executeQuery("SELECT ID, NOME, SENHA, STATUS, DT_CRIACAO  FROM dbo.USUARIOS WHERE LOGIN=?", loginUsuario);
        Usuario usuario = new Usuario();
        while (rs.next()){
            usuario.setId(rs.getInt("ID"));
            usuario.setNome(rs.getString("NOME"));
            usuario.setSenha(rs.getString("SENHA"));
            usuario.setStatus(rs.getInt("STATUS"));
            usuario.setDt_criacao(rs.getDate("DT_CRIACAO"));
        }
        usuario.setLogin(loginUsuario);
        rs.close();
        return usuario;
    }   
    
}
